<?php
include 'common/autoload.php';
include 'class/SmsTpl.class.php';
$smstpl=new SmsTpl($GLOBALS['le']);
// 异步获取对应的景点门票
if($_SERVER['HTTP_REQUEST_TYPE']=='getTicket'){
	$lid = $_REQUEST['landid']+0;
	$sql="select t.id,t.title,l.addtime,t.pid,p.apply_did from uu_jq_ticket t left join uu_land l on t.landid = l.id left join uu_products p on p.id=t.pid where t.landid=$lid and p.apply_limit=1 and p_status=0";
	$GLOBALS['le']->query($sql);
	$tickes = array();
	while($row=$GLOBALS['le']->fetch_assoc()){
		$row['ids'] = $row['apply_did'].'|'.$lid.'|'.$row['id'];
		$tickes[]=$row;
	}
	echo count($tickes) ? 
	json_encode(array('s'=>'success','content'=>$tickes)):json_encode(array('s'=>'fail'));
	exit;
}

include 'common/header.html';
include 'common/left.html';
include 'common/page.php';
include 'common/area.inc.php';
include 'common/Prod_Common.php';
$prolist = array();
$sql="SELECT id,getPY(title,1) as py,title FROM uu_land";
$GLOBALS['le']->query($sql);
$prolist=array();
while($row=$GLOBALS['le']->fetch_assoc()){
	$title = mb_strlen($row['title'])>20 ? mb_substr($row['title'],0,20,'utf8'):$row['title'];
	$prolist[$row['id']]=$row['py'].'-'.$title;
}
asort($prolist);
?>
<link rel="stylesheet" href="css/tao.css"/>
<link rel="stylesheet" href="css/page.css" media="all" />
<link rel="stylesheet" href="css/plist.css" media="all" />
<link rel="stylesheet" href="http://www.16u.cc/jq/css/aliceui.min.css"/>
<link rel="stylesheet" href="http://www.16u.cc/d/css/resetui.css"/>
<script src="js/select2.js"></script>
<link rel="stylesheet" type="text/css" href="css/select2.css">
<link rel="stylesheet" href="css/smsconfig.css?body=1"/>
<div class="r_content" id="bind_tao">
    <h3 class="mc_title">
        <span class="mctit_1"><span class="mctit_2">短信模板</span></span>
    </h3>
<!--     <div class="container" style="padding-left;10px;";>
        请输入搜索的产品名称：
        <div class="input-wrap">
            <input type="hidden" name="did" value="<?=$did?>"/>
            <input type="hidden" name="ds_name" value="<?=$m_info['dname']?>"/>
            <input type="text" <?=$disable?> placeholder="请输入搜索的产品名称" name="coop" id="coop" value="<?=$valiRs['seller_nick']?>">
            <button type="submit" <?=$disable?> id="checkSubmitBtn" class="chkbtn">搜&nbsp;&nbsp;&nbsp;索</button>
        </div>
    </div> -->


	<div class="jingqu">
		<span>产品：</span>
		<select name="prodname" id="land">
			<option value="">----请选择----</option>
			<?foreach($prolist as $k => $v):?>
			<option value="<?=$k?>"><?=$v?>-<?=$k?></option>
			<?endforeach;?>
		</select>
	</div>
	<script>
		$("#land").select2({
			placeholder: "Select a State",
			allowClear: true
		});
	</script>
	<div class="ticket_list_wrap">
		<table class="list_table">
			<thead>
			<tr>
				<th>产品ID</th>
				<th>门票名称</th>
				<th>添加时间</th>
				<th>操作</th>
			</tr>
			</thead>
			<tbody>

			</tbody>
		</table>
	</div>

		<div class="ui-grid-row" id="">
            <h3>编辑短信模板内容：</h3>
            <ul class="tpl_variables">
                <li class="line">短信标签：</li>
                <!-- <li><a href="javascript:" class="var" data-code="{dname}" data-txt="[商家名称]"><em>+</em>[商家名称]</a></li> -->
				<li><a href="javascript:" class="var" data-code="{pname}" data-txt="[预定产品]"><em>+</em>[预定产品]</a></li>
				<li><a href="javascript:" class="var" data-code="{tnum}" data-txt="[预定数量]"><em>+</em>[预定数量]</a></li>
				<li><a href="javascript:" class="var" data-code="{code}" data-txt="[凭证号]"><em>+</em>[凭证号]</a></li>
				<li><a href="javascript:" class="var" data-code="{begintime}" data-txt="[游玩/入住时间]"><em>+</em>[游玩/入住时间]</a></li>
				<li><a href="javascript:" class="var" data-code="{getaddr}" data-txt="[取票信息]"><em>+</em>[取票信息]</a></li>
				<!-- <li><a href="javascript:" class="var" data-code="{sphone}" data-txt="[客服电话]"><em>+</em>[客服电话]</a></li> -->
				<!-- <li><a href="javascript:" class="var" data-code="{place}" data-txt="[集合地点]"><em>+</em>[集合地点]</a></li> -->
				<li><a href="javascript:" class="var" data-code="{link}" data-txt="[详情及二维码链接]"><em>+</em>[详情及二维码链接]</a></li>
				<!-- <li><a href="javascript:" class="var" data-code="{maintain}" data-txt="[预定天数]"><em>+</em>[预定天数]</a></li> -->
                <li class="line">短信内容：</li>
				<li class="red">请严格按照下方短信模板的顺序进行编辑，否则可能导致短信模板无法正确配置！</li>
            </ul>    
			 <div id="sms_content" contenteditable="true" onclick="getPos();" onkeyup="getPos();" class="defaultTpl" data-sid='' data-pid='' data-lid=''>

			</div>
			<textarea class="tmpSms" style="display:none;"></textarea>
        </div>

		<div class="btnWrap">
            <button type="button" id="getDefault" class="ui-button ui-button-lblue">获取默认模板</button>
            <button type="button" id="resetTpl" class="ui-button ui-button-lblue">重置短信模板</button>
            <button type="button" id="submitPost" class="ui-button ui-button-lwhite">保存短信模板</button>
            <img id="wait_img" style="display: none;" src="http://www.16u.cc/jq/images/loading_smal.gif" alt="数据保存中..."/>
        </div>
		<div class="ui-grid-row" id="">
            <h3>短信默认模板：</h3>
			<div class="defaultTpla" id="defaultTpl">
				凭证号:<span contenteditable="false" data-code="{code}">[凭证号]</span>
				,您已成功购买了
				<span contenteditable="false" data-code="{pname}">[预定产品]</span><span contenteditable="false" data-code="{tnum}">[预定数量]</span>
				,
				<span contenteditable="false" data-code="{begintime}">[游玩/入住时间]</span>
				,<span contenteditable="false" data-code="{getaddr}">[取票信息]</span>
				,此为入园凭证,请妥善保管。
				<span contenteditable="false" data-code="{link}">详情及二维码:[详情及二维码链接]</span>
			<!--
			<span contenteditable="false" data-code="{pname}">[预定产品]</span>:<span contenteditable="false" data-code="{tnum}">[预定数量]</span>凭证号:<span contenteditable="false" data-code="{code}">[凭证号]</span>,[游玩日期|有效期至|有效期]:<span contenteditable="false" data-code="{begintime}">[游玩/入住时间]</span>,取票信息:<span contenteditable="false" data-code="{getaddr}">[取票信息]</span>,此为入园凭证,请妥善保管。<span contenteditable="false" data-code="{link}">详情及二维码:[详情及二维码链接]</span>
			-->
			</div>
        </div>
	</div>

<style>
.ticket_list_wrap,.sms_wrap{border:1px solid #CCCCCC;margin-top:10px;padding-bottom:10px;}
.list_table{width:100%;}
.list_table thead{background: #f5f4f4;line-height: 35px;}
.list_table th,td{text-align:center;}

.sms_wrap p{padding:5px;}
.sms_wrap textarea{width:98%;height:150px;margin:0 1%;resize:none;margin-bottom:10px;}
.btn{display:block;width:126px;height:32px;margin:0 auto;text-align:center;line-height:32px;background:#F57403;color:#FFF;border-radius:4px;border:none;cursor:pointer;font-weight:bold;}
.btn:hover{background:#E56F03;}
.product_i{color:#008EC1;}
.red{color:red;}
#s2id_land{width:300px};
</style>
<script type="text/javascript" src="js/smsconfig_dev.js?body=1"></script>
<script>
$(document).ready(function(){

	$("#land").change(function(){
		landid=$(this).val();
		$("#sms_content").attr("data-lid",landid);
		$("#sms_content").attr("data-sid","");
		$("#sms_content").attr("data-pid","");
		if(landid==''){
			_options = '<option value="">--选择--</option>';
			$("#ticket").html(_options);
			return;
		}
		$(".product_i").html($(this).attr("data"));
		$.ajax({
			uri:window.location.href,
			data:"landid="+landid,
			beforeSend:function (XMLHttpRequest) {
				XMLHttpRequest.setRequestHeader("request_type","getTicket");
			},
			success: function(data){
				data = JSON.parse(data);
				if(data.s=='success'){
					htmlTxt='';
					for(var i in data.content){
						if(i=='toJSONString') continue;
						htmlTxt+='<tr data="'+data.content[i].title+'">';
						htmlTxt+='<td>'+data.content[i].ids+'</td>';
						htmlTxt+='<td>'+data.content[i].title+'</td>';
						htmlTxt+='<td>'+data.content[i].addtime+'</td>';
						htmlTxt+='<td><a href="javascript:void(0);" data="'+data.content[i].pid+'" class="choice">选择</a></td>';
						htmlTxt+= '</tr>';
					}
					$(".list_table tbody").html(htmlTxt);
				}
			}
		});
	});
	// 选择编辑门票
	$(document).on("click",".choice",function(){
		$ttitle=$(this).parents("tr").attr("data");
		pid = $(this).attr("data");
		$("#smsContent").attr("data-pid",pid);
		$.post('call/jh_sms.php',{pid:pid,action:"getSms"}).done(function(response){
                data = JSON.parse(response);
				if(data.s=='success'){
					$("#sms_content").html(data.content);
					$("#sms_content").attr("data-sid",data.id);
					$("#sms_content").attr("data-pid",data.pid);
					$(".tmpSms").val(data.content);
				}
        });
	});
	
	//保存模板	
	$("#submitPost").on('click',function(){
		pid = $("#sms_content").attr("data-pid");
		if(pid==""){
		 alert("pid为空无法保存")
		 return false;
		}
		lid = $("#sms_content").attr("data-lid");
		sid = $("#sms_content").attr("data-sid");
		sms = w.remove_link($("#sms_content").html());
        var t = $(this);
        t.attr('disabled','disabled').text('数据保存中...').next('#wait_img').show();
        $.post('call/jh_sms.php',{pid:pid,lid:lid,sid:sid,sms:sms,action:"saveSms"}).done(function(response){
                alert(response);
            }).always(function(){
                t.removeAttr('disabled').text('保存短信模板').next('#wait_img').hide();
        });
    });

});

</script>
<?
include 'common/footer.html';
?>